package com.kaiwukj.android.ufamily.mvp.ui.page.home.home.active;

import android.widget.ImageView;

import com.blankj.utilcode.util.StringUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.ActiveResult;
import com.kaiwukj.android.ufamily.utils.FormatUtils;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class ActiveAdapter extends BaseQuickAdapter<ActiveResult, BaseViewHolder> {
    public ActiveAdapter() {
        super(R.layout.item_active, new ArrayList<>());
    }

    @Override
    public long getItemId(int position) {
        ActiveResult result = getItem(position);
        return result == null ? -1 : result.getId();
    }

    public void postJoin(int id, boolean join) {
        int position = getPositionById(id);
        if (position < 0) return;
        int oldNum = getItem(position).getJoinNum();
        if (join) {
            getItem(position).setJoinNum(oldNum + 1);
        } else {
            if (oldNum > 0) {
                getItem(position).setJoinNum(oldNum - 1);
            }
        }

        notifyItemChanged(position, "join");

    }

    public int getPositionById(long id) {
        for (int i = 0; i < getData().size(); i++) {
            if (id == getData().get(i).getId()) {
                return i;
            }
        }
        return -1;
    }

    @Override
    protected void convert(@NotNull BaseViewHolder holder, ActiveResult item, @NotNull List<?> payloads) {
        if (payloads.isEmpty()) {
            super.convert(holder, item, payloads);
        } else {
            String tag = (String) payloads.get(0);
            if ("join".equals(tag)) {
                holder.setText(R.id.tv_hot, String.format("热度 %s", item.getJoinNum() == 0 ? 2341 : item.getJoinNum() * 2341));
            }
        }
    }

    @Override
    protected void convert(@NotNull BaseViewHolder helper, ActiveResult item) {
        String img = "";
        if (!StringUtils.isEmpty(item.getActivityImg())) {
            String[] imgs = item.getActivityImg().split(",");
            if (imgs.length > 0) {
                img = imgs[0];
            }
        }
        ImageView ivImage = helper.getView(R.id.iv_image);
        Glide.with(getContext())
                .load(img)
                .placeholder(R.drawable.picture_image_placeholder)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(ivImage);
        helper.setText(R.id.tv_title, item.getActivityTitle());
      /*  helper.setText(R.id.tv_time, String.format("报名时间: %s-%s",
                FormatUtils.formatTime(item.getActivityStartTime(), "MM.dd"),
                FormatUtils.formatTime(item.getActivityEndTime(), "MM.dd")));*/

        helper.setText(R.id.tv_time, String.format("报名截止: %s", FormatUtils.formatTime(item.getEnrollDeadline())));

        helper.setText(R.id.tv_hot, String.format("热度 %s", item.getJoinNum() == 0 ? 2341 : item.getJoinNum() * 2341));
    }
}
